package cn.itsource.pethome.system.role.service.impl;

import cn.itsource.pethome.base.query.BaseQuery;
import cn.itsource.pethome.system.role.domain.Role;
import cn.itsource.pethome.system.role.mapper.RoleMapper;
import cn.itsource.pethome.system.role.service.IRoleService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-02-01
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        //先添加role获取id
        super.add(role);
        /* //新建类，循环添加
        List<Long> menus = role.getMenus();
        if(!menus.isEmpty()){
            menus.forEach(menuId->{//循环的意思
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(menuId);
                roleMapper.addRoleMenu(roleMenu);
            });
        }*/
        executeAdd(role, role.getId());
    }

    //执行添加（同时添加角色-菜单，角色-权限表的数据）
    private void executeAdd(Role role, Long roleId) {
        //获取全部菜单id，添加数据库
        List<Long> menuIds = role.getMenus();
        if (!menuIds.isEmpty()){
            List<Map<String, Long>> roleMenus = new ArrayList<>();
            for (Long menuId : menuIds) {
                HashMap<String, Long> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleMenus.add(map);
            }
            roleMapper.addMenus(roleMenus);
        }
        //获取全部权限id，添加数据库
        List<Long> permissionIds = role.getPermissions();
        if (!permissionIds.isEmpty()){
            List<Map<String,Long>> rolePer = new ArrayList<>();
            for (Long permissionId : permissionIds) {
                HashMap<String, Long> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId",permissionId);
                rolePer.add(map);
            }
            roleMapper.addPermissions(rolePer);
        }
    }

    @Override
    public void delete(Long id) {
        //有外键约束，先删除中间表的数据
        roleMapper.delMenuByRoleId(id);
        roleMapper.delPermissionByRoleId(id);
        super.delete(id);
    }

    @Override
    public void update(Role role) {
        //修改就是先删除再添加
        super.update(role);
        roleMapper.delMenuByRoleId(role.getId());
        roleMapper.delPermissionByRoleId(role.getId());
        executeAdd(role, role.getId());
    }

    @Override
    public List<Role> findQuery(BaseQuery query) {
        List<Role> query1 = super.findQuery(query);
        //循环获取每一个角色id，根据角色id分别查询菜单和权限
        query1.forEach(role-> {
            Long id = role.getId();
            role.setMenuList(roleMapper.getMenuByRoleId(id));
            role.setPermissionList(roleMapper.getPermissionByRoleId(id));
        });
        return query1;
    }
}
